﻿using SRB.Tg.Common;
using SRB.Tg.DrawingStandard;
using SRB.Tg.Operate;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Teigha.DatabaseServices;
using Teigha.Geometry;

namespace SRB.Tg.Model
{
    public class DrawingBridgeStructure
    {

        BridgeStructureType bridge_type = BridgeStructureType.AviationBridge;
        System.Data.DataTable dtProject = new System.Data.DataTable();
        System.Data.DataTable dtBridge = new System.Data.DataTable();
        System.Data.DataTable dtBridgeDrawing = new System.Data.DataTable();
        System.Data.DataTable dtProjectConfig = new System.Data.DataTable();
        string _bridgeId = null;
        DrawingConfigModel drawingConfigModel = new DrawingConfigModel();
       
        public DrawingBridgeStructure(string bridgeId, BridgeStructureType type,out System.Data.DataTable dtProjectTb, bool isMerge = false)
        {
            bridge_type = type;
            #region  参数数据初始化
            dtBridgeDrawing = new System.Data.DataTable();//图纸目录信息表
            //传入bridgeId，查询出mainId
            string sqlMainId = "select * from busi_bridge where id='" + bridgeId + "' ";
            string mainId = MySqlHelper.ExecuteDataTable(sqlMainId, CommandType.Text).Rows[0]["main_id"].ToString();
            _bridgeId = mainId;
            string sqlBridge = "select * from busi_bridge_main where id='" + mainId + "'";
            dtBridge = MySqlHelper.ExecuteDataTable(sqlBridge, CommandType.Text);

            int bridge_scope = 1;//桥幅（单幅桥和双幅桥）
            if (dtBridge.Rows.Count > 0)
            {
                bridge_scope = int.Parse(dtBridge.Rows[0]["bridge_scope"].ToString());
                if (bridge_scope > 0)
                    isMerge = dtBridge.Rows[0]["drawing_level"].ToString() == "1" ? true : false; //双幅桥图纸是否合并
            }


            string sqlDrawing = "select * from busi_bridge_catalog where  bridge_main_id='" + mainId + "'";
            dtBridgeDrawing = MySqlHelper.ExecuteDataTable(sqlDrawing, CommandType.Text);
            #endregion

            dtProjectTb = dtBridgeDrawing;
        }
        public void DrawingBridgeStructureGA(Database db, int OverAllScale, string bridgeId, out System.Data.DataTable dtProjectConfigTb,string catalogid)
        {
            try
            {
                string sqlBridgeConfig = "select * from busi_project_config where bridge_id='" + bridgeId + "'";
                dtProjectConfig = MySqlHelper.ExecuteDataTable(sqlBridgeConfig, CommandType.Text);
                PictureFrameTools.GetBridgeConfig(dtProjectConfig);

                switch (bridge_type)
                {
                    case BridgeStructureType.AviationBridge:
                        #region 绘制图形
                        drawingConfigModel = PictureFrameTools.GetDrawingConfig(dtBridgeDrawing, catalogid, dtProjectConfig);
                        OverAllScale = drawingConfigModel.drawing_scale;

                        Extents2d ExtBridgeE, ExtBridgeP, ExtBridgeSection, ExtNote;
                        Point2d RefPtBridgeE, RefPtBridgeP, RefPtBridgeSectioon, RefPtNote;
                        RefPtBridgeE = Point2d.Origin;
                        ExtBridgeE = new Extents2d();
                     
                        ExtBridgeP = new Extents2d();
                        RefPtBridgeSectioon = Point2d.Origin;
                        ExtBridgeSection = new Extents2d();
                        RefPtNote = Point2d.Origin;
                        ExtNote = new Extents2d();

                        DrawingAviationBridge bridge = new DrawingAviationBridge(_bridgeId, OverAllScale);
                        Point2d RefPt;
                        DrawingAviationBridge.PlotElevation(ref db, out ExtBridgeE, RefPtBridgeE, out RefPt);
                        double H = ExtBridgeE.L() / OverAllScale;
                        if (ExtBridgeE.L()/ OverAllScale>100)
                            RefPtBridgeP = ExtBridgeE.MinPoint.C2D(0, -20 * OverAllScale);
                        else
                            RefPtBridgeP = ExtBridgeE.MinPoint.C2D(0,-45* OverAllScale);
                        DrawingAviationBridge.PlotPlan(ref db, ref ExtBridgeE, RefPtBridgeP);
                        H = ExtBridgeE.L() / OverAllScale;
                        if (ExtBridgeE.L() / OverAllScale > 180)
                            RefPtNote = new Point2d(RefPt.X, ExtBridgeE.MinPoint.Y - 12.5* OverAllScale);
                        else
                            RefPtNote = new Point2d(RefPt.X, ExtBridgeE.MinPoint.Y - 25 * OverAllScale);
                        DrawingAviationBridge.PlotTable(ref db, ref ExtBridgeE, RefPtNote);
                        RefPtBridgeSectioon = ExtBridgeE.MinPoint.C2D(ExtBridgeE.W()*0.75, -10* OverAllScale);
                        DrawingAviationBridge.PlotDetail(ref db, out ExtBridgeSection,RefPtBridgeSectioon, OverAllScale);
                        ExtBridgeE= ExtBridgeE.Add(ExtBridgeSection);

                        RefPtBridgeSectioon = ExtBridgeE.MinPoint.C2D(0, -297*OverAllScale);
                        ExtBridgeSection = new Extents2d(RefPtBridgeSectioon, RefPtBridgeSectioon);
                        DrawingAviationBridge.PlotDetailNew(ref db, out ExtBridgeSection, RefPtBridgeSectioon, 80);
                        #endregion

                        #region 模型空间到布局空间投影处理
                        LayoutTools.CreateAviationBridgeLayout(db, "图纸", ExtBridgeE,  ExtBridgeSection, Extensions.leftWidth, Extensions.rightWidth, Extensions.bottomHeight, Extensions.topHeight, OverAllScale,  2);
                        Point2d tKPoint = Point2d.Origin;
                        //for (int i = 0; i < Extbeam_lst.Count; i++)
                        //{
                        //    Extents2d ex = new Extents2d();

                        //    TextPloter.AddPaperDBText(db, tKPoint.C3D(420 * i + 161, 12.5), ref ex, dtProject.Rows[1][1].ToString(), 1, 4, "SRB.HT", 0, TextHorizontalMode.TextCenter);

                        //    TextPloter.AddPaperDBText(db, tKPoint.C3D(420 * i + 366, 12.5), ref ex, dtProject.Rows[1][2].ToString(), 1, 4, "SRB.HT", 0, TextHorizontalMode.TextCenter);
                        //    //TextPloter.AddPaperDBText(db,tKPoint.C3D(420 * i + 400, 12.5), ref ex, System.DateTime.Now.ToString("yyyy.MM"), 1, 4.5, "SRB.HT", 0, TextHorizontalMode.TextCenter);
                        //    TextPloter.AddPaperDBText(db, tKPoint.C3D(420 * i + 420 - 33, 297 - 16.5), ref ex, (i + 1).ToString(), 1, 3.5, "SRB.HT", 0, TextHorizontalMode.TextCenter);
                        //    TextPloter.AddPaperDBText(db, tKPoint.C3D(420 * i + 420 - 16.5, 297 - 16.5), ref ex, (Extbeam_lst.Count).ToString(), 1, 3.5, "SRB.HT", 0, TextHorizontalMode.TextCenter);
                        //}
                        Extents2d ex = new Extents2d();
                        PictureFrameTools.AddPaperDBText(db, tKPoint, 0, drawingConfigModel, ref ex,   1, 2);
                        PictureFrameTools.AddPaperDBText(db, tKPoint, 1, drawingConfigModel, ref ex, 2, 2);
                        #endregion


                        break;

                    case BridgeStructureType.BeamBridge:

                        break;

                    
                }
            }
            catch (Exception ex)
            {

            }
            dtProjectConfigTb = dtProjectConfig;
        }
    }

    
}
